package com.qf.fmall2204.wxpay;

import cn.hutool.core.util.XmlUtil;
import com.qf.fmall2204.constants.Constants;
import com.qf.fmall2204.orders.service.IOrdersService;
import com.qf.fmall2204.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class WxPayController {

    @Autowired
    IOrdersService ordersService;

    /**
     * 微信支付回调接口
     * 注意：请求体中放的是 xml字符串
     * @param xml
     * @return
     */
    @PostMapping("/callback")
    public ResultVo callback(@RequestBody String xml) throws IOException {
        System.out.println(xml);
        // xml -> map
        Map<String, Object> map = XmlUtil.xmlToMap(xml);
        String orderId = (String) map.get("out_trade_no");

        ordersService.paySuccess(orderId);

        return new ResultVo(Constants.SUCCESS_CODE, "success", null);
    }

}
